package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDateList(begin, end);
        List<Double> turnList = new ArrayList<>();
        for (LocalDate date : dateList) {
            Map map = getMap(date);
            map.put("status", Orders.COMPLETED);//订单状态为 完成的
            Double turnOver = orderMapper.sunByMap(map);
            turnOver = turnOver == null ? 0.0 : turnOver;//如果没有查到就赋值为0
            turnList.add(turnOver);
        }

        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnList, ","))
                .build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDateList(begin, end);
        //用户总量
        List<Double> userList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        for (LocalDate date : dateList) {
            Map map = getMap(date);
            //所有用户
            Double turnOver = userMapper.countByMap(map);
            Integer newTurnOver = userMapper.newCountByMap(map);
            turnOver = turnOver == null ? 0.0 : turnOver;//如果没有查到就赋值为0
            newTurnOver = newTurnOver == null ? 0 : newTurnOver;//如果没有查到就赋值为0
            userList.add(turnOver);
            newUserList.add(newTurnOver);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(userList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDateList(begin, end);
        List<Integer> everyValidOrderList = new ArrayList<>();//每日有效订单数
        List<Integer> everyOrderList = new ArrayList<>();//每日有效订单数
        for (LocalDate date : dateList) {
            Map map = getMap(date);
            map.put("status", Orders.COMPLETED);
            Integer count = orderMapper.getEveryOrderCount(map);//每日有效订单数
            Map map1 = getMap(date);//基本map
            Integer count1 = orderMapper.getEveryOrderCount(map1);//每日订单数
            everyValidOrderList.add(count);
            everyOrderList.add(count1);
        }
        Integer ValidCount = orderMapper.getValidCount();//有效订单数
        Integer totalOrderCount = orderMapper.getTotalOrderCount();//订单总数
        Double OrderComPletionRate = (ValidCount + 0.0) / totalOrderCount;//订单完成率
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .validOrderCountList(StringUtils.join(everyValidOrderList, ","))
                .orderCountList(StringUtils.join(everyOrderList, ","))
                .validOrderCount(ValidCount)
                .totalOrderCount(totalOrderCount)
                .orderCompletionRate(OrderComPletionRate)
                .build();
    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        List<String> nameList = orderDetailMapper.getNameList();//商品名列表

        List<Integer> numberList = new ArrayList<>();
        for (String orderName : nameList) {
            Integer count = orderDetailMapper.getNumberList(orderName);//获取单品的总量
            numberList.add(count);
        }
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }


    //获取日期列表
    public List<LocalDate> getDateList(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        //直到加到最后一天
        while (!begin.equals(end)) {
            //日期 +1
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }

    //获取基本的map集合
    public Map getMap(LocalDate date) {
        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);//获取的是 0：00  当天的开始
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);//23：59：5999999999 当天的快结束
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        return map;
    }
}
